package dtd.PHS.VND_ExchangeRate.data;

import java.util.HashMap;

import android.content.Context;

import dtd.PHS.VND_ExchangeRate.R;
import dtd.PHS.VND_ExchangeRate.utils.ApplicationContext;
import dtd.PHS.VND_ExchangeRate.utils.Helpers;
import dtd.PHS.VND_ExchangeRate.utils.Logger;


public class RateItem {
	static public enum MyCurrency {USD,EUR,AUD,CAD,CHF,GBP,HKD,JPY,SGD,THB,VND}

	private static final String SEPERATOR = "/";

	private MyCurrency currency;
	private double buying;
	private double transfer;

	private double selling;
	public RateItem(MyCurrency c, double buying, double transfer, double selling) {
		init(c, buying, transfer, selling);
	}

	private void init(MyCurrency c, double buying, double transfer,	double selling) {
		this.currency = c;
		this.buying = buying;
		this.transfer = transfer;
		this.selling = selling;
	}

	public RateItem(String currencyName, double buying, double transfer, double selling) {
		MyCurrency c = getCurrencyFromName(currencyName);
		init( c,buying,transfer,selling );
	}

	//"AUD/21875.68/22007.73/22349.28"
	public RateItem(String rateStr) {
		String[] words = rateStr.split(SEPERATOR);
		this.currency = getCurrencyFromName(words[0]);
		
		this.buying = Double.parseDouble(words[1].trim());
		this.transfer = Double.parseDouble(words[2].trim());
		this.selling = Double.parseDouble(words[3].trim());
	}

	private static MyCurrency getCurrencyFromName(String currencyName) {
		boolean found = false;
		MyCurrency currency = null;
		for(MyCurrency c : MyCurrency.values()) {
			if ( c.toString().equals(currencyName.trim())) {
				currency = c;
				found = true;				
			}
		}
		
		if ( ! found ) {
			Logger.logError(new Exception("Currency not found : ==" + currencyName+"=="));
		}
		
		return currency;
	}
	
	public MyCurrency getCurrency() {
		return currency;
	}
	public double getBuying() {
		return buying;
	}
	public double getTransfer() {
		return transfer;
	}
	public double getSelling() {
		return selling;
	}

	@Override
	public String toString() {
		return currency.toString() + SEPERATOR + buying + SEPERATOR + transfer + SEPERATOR + selling;
	}
	
	

	public String getCurrencyName() {
		return currency.toString();
	}

	public static long changeFromTo(String fromCurrency, String toCurrency, long amount) {
		if ( fromCurrency.equals(toCurrency)) return amount;
		if ( toCurrency.equals("VND")) {
			RateItem item = DataProvider.getInstance().getRate(fromCurrency);
			double rate = item.getTransfer();
			return Math.round(rate * amount);
		}
		
		if ( fromCurrency.equals("VND")) {
			RateItem item = DataProvider.getInstance().getRate(toCurrency);
			double rate = item.getTransfer();
			return Math.round((1.0/rate) * amount);
		}
		
		long newAmount = changeFromTo(fromCurrency, "VND", amount);
		return changeFromTo("VND", toCurrency, newAmount);
	}

	public int getFlagResource() {
		return Helpers.getFlagIconName(getCurrencyName());
	}

	public String getFullName() {
		Context context = ApplicationContext.getContext();
		String[] abbr = context.getResources().getStringArray(R.array.currencies_abb);
		String[] full = context.getResources().getStringArray(R.array.currencies_full);
		for(int i = 0 ; i < abbr.length ; i++) {
			if ( currency.toString().equals(abbr[i])) {
				return full[i];
			}
		}
		
		return "";
		
	}
}
